<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2008 Starbugs <univis2typo3@googlegroups.com>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

require_once(PATH_tslib.'class.tslib_pibase.php');
require_once(t3lib_extMgm::extPath('univis2typo3') . 'common/class.tx_univis2typo3_person.php');

/**
 * Plugin 'personal page' for the 'univis2typo3' extension.
 *
 * @author	Starbugs <univis2typo3@googlegroups.com>
 * @package	TYPO3
 * @subpackage	tx_univis2typo3
 */
class tx_univis2typo3_pi11 extends tslib_pibase {
	var $prefixId      = 'tx_univis2typo3_pi11';		// Same as class name
	var $scriptRelPath = 'pi11/class.tx_univis2typo3_pi11.php';	// Path to this script relative to the extension dir.
	var $extKey        = 'univis2typo3';	// The extension key.

	var $department = '';
	var $p = null; // holder for person data

	/**
	 * The main method of the PlugIn
	 *
	 * @param	string		$content: The PlugIn content
	 * @param	array		$conf: The PlugIn configuration
	 * @return	The content that is displayed on the website
	 */
	function main($content='',$conf='') {
		$this->conf = $conf;
		$this->pi_setPiVarDefaults();
		$this->pi_loadLL();
		$this->pi_USER_INT_obj = 1;	// Configuring so caching is not expected. This value means that no cHash params are ever set. We do this, because it's a USER_INT object!

		$page_select = t3lib_div::makeInstance("t3lib_pageSelect");
		$page_select->init(false);

		$this->department = $this->conf['department_id'];
		$this->templateTotal = $this->cObj->fileResource($this->conf['templateFile']);
		$this->template = $this->cObj->getSubpart($this->templateTotal, "###TEMPLATE_PERSONAL_PAGE_HDR###");
		$this->language = $GLOBALS["TSFE"]->sys_language_uid;

		$personHelper = t3lib_div::makeInstance("tx_univis2typo3_person", $conf);
		$this->p = $personHelper->loadPersonData();

		$title_name = $this->p->personName;
		if ($this->p->btitle) $title_name = $this->p->btitle . ' ' . $title_name;
		if ($this->p->atitle) $title_name .= ', ' . $this->p->atitle;

		$office_hours = '';
		if (!empty($this->p->officeHours) ) {
			$differentRoom = $this->p->officeHours['room'] != $this->p->office;
			$office_hours = '<div class="officehours">';
			if (1==$this->language) {
				$office_hours .= 'Sprechstunde:'
					.($differentRoom ? '<br />Datum:' : '')
					.' '.$this->p->officeHours['day'].', '.$this->p->officeHours['start'].' - '.$this->p->officeHours['end'].' Uhr<br />'
					.($differentRoom ? 'Zimmer: '.$this->p->officeHours['room'].'<br />' : '');
			} else {
				$office_hours .= 'Office hours:'
					.($differentRoom ? '<br />' : ' ')
					. $this->p->officeHours['day'].', '.$this->p->officeHours['start'].' - '.$this->p->officeHours['end'].'<br />'
					.($differentRoom ? 'Room: '.$this->p->officeHours['room'].'<br />' : '');
			}
			$office_hours .= ($this->p->officeHours['comment'] ? '<i>('.$this->p->officeHours['comment'].')</i><br />' : '')
				. '</div>';
		}


        // Render the image 
		// Set some configuration parameters for the image rendering 
		$lconf['file'] = $this->p->imgUrl;
		if($lconf['file'] == "")
			$lconf['file'] = "fileadmin/templates/img/nopic.png";
		$lconf['file.']['maxH'] = "180";
		$lconf['file.']['maxW']  = "300";
		$image = $this->cObj->IMAGE($lconf);
		
		$markers = array();
		$markers['###T_PP_OFFICEH###']    = $office_hours;
		$markers['###T_PP_FUNC###']       = $this->linkGroupName($this->p->group, $this->p->groupUrl);
		$markers['###T_PP_IMG###']        = $image;//$this->p->imgUrl;
		$markers['###T_PP_FIRST_NAME###'] = $this->p->firstName;
		$markers['###T_PP_LAST_NAME###']  = $this->p->lastName;
		$markers['###T_PP_FULLNAME###']   = $this->p->personName;
		$markers['###T_PP_TITLE_NAME###'] = $title_name;
		$markers['###T_PP_FULLNAME###']   = $this->p->personName;

		$content = $this->cObj->substituteMarkerArray($this->template, $markers);
		return $this->pi_wrapInBaseClass($content);
	}

	/**
	 * @param sring $str Some text
	 * @return string Same text as input, but with LME group names linked to group page
	 */
	function linkGroupName($str, $url) {
		/* TODO: implement */
		$conf = array(
  			// Link to current page
  			'parameter' => $url,
  			// We must add cHash because we use parameters
  			'useCashHash' => true,
    		// We want link only
  			//'returnLast' => 'url',
		);
		return $this->cObj->typoLink($str, $conf);
	}

	function getName() {
	    return $this->p->personName;
	}
}


if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/univis2typo3/pi11/class.tx_univis2typo3_pi11.php']) {
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/univis2typo3/pi11/class.tx_univis2typo3_pi11.php']);
}

?>
